package com.engine.qz.controller;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.qz.service.CustomRosterService;
import com.engine.qz.service.impl.CustomRosterServiceImpl;
import com.engine.qz.util.response.ReturnResult;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Map;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2025/3/10
 * @Description ${description}
 * @Author Lee
 */
public class CustomRosterController {
    private CustomRosterService getCustomRosterService(User user) {
        return ServiceUtil.getService(CustomRosterServiceImpl.class, user);
    }

    @GET
    @Path("/listPage")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult listPage(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            return ReturnResult.successed(user, getCustomRosterService(user).listPage(map));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    @GET
    @Path("/customFields")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult customFields(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(user, getCustomRosterService(user).customFieldMap());
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    @GET
    @Path("/customExport")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response customExport(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            return getCustomRosterService(user).customExport(map);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 高级搜索条件
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getSearchCondition")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getSearchCondition(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            return ReturnResult.successed(user, JSON.toJSON(getCustomRosterService(user).getSearchCondition(map)));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }
}
